package com.lulalei.dbviewer.db.mysql.service;

import com.lulalei.dbviewer.commons.bean.DBInfo;
import com.lulalei.dbviewer.commons.bean.JDBCConnectBean;
import com.lulalei.dbviewer.commons.bean.RowBean;
import com.lulalei.dbviewer.commons.bean.TableBean;
import com.lulalei.dbviewer.commons.exception.CustomizeException;
import com.lulalei.dbviewer.commons.utils.NameCovertUtils;
import com.lulalei.dbviewer.db.DBDao;
import com.lulalei.dbviewer.db.DBService;
import com.lulalei.dbviewer.db.mysql.DTO.ColumnsQueryDTO;
import com.lulalei.dbviewer.db.mysql.DTO.TableAndColumnsQueryDTO;
import com.lulalei.dbviewer.dto.DBConnectDTO;
import com.lulalei.dbviewer.dto.DBTablesInfoDTO;
import com.lulalei.dbviewer.vo.PageVO;
import com.lulalei.dbviewer.vo.TablesInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.sql.SQLException;
import java.util.List;

@Component
public class MySQLService implements DBService {

    @Autowired
    @Qualifier("mySqlDao")
    private DBDao dbDao;

    @Override
    public boolean connect(DBConnectDTO connectDTO) throws CustomizeException, SQLException {
        new JDBCConnectBean(toDBInfo(connectDTO), connectDTO.getTest());
        return true;
    }

    @Override
    public PageVO<TableBean> getTableInfo(DBTablesInfoDTO dbTablesInfoDTO) throws CustomizeException, SQLException {

        TablesInfoVO pageVO = new TablesInfoVO();
        pageVO.set(dbTablesInfoDTO);
        pageVO.setTotal(dbDao.getTableTotal(dbTablesInfoDTO));
        pageVO.setData(dbDao.getTables(dbTablesInfoDTO));

        return pageVO;
    }

    @Override
    public List<TableBean> getTableAndField(TableAndColumnsQueryDTO queryDTO) throws CustomizeException, SQLException {
        DBTablesInfoDTO dbTablesInfoDTO = new DBTablesInfoDTO();
        dbTablesInfoDTO.setConnectionName(queryDTO.getConnectionName());
        dbTablesInfoDTO.setTableNames(queryDTO.getTableNames());
        //获取需要生成的表信息
        List<TableBean> tables = dbDao.getTables(dbTablesInfoDTO);

        //遍历表信息，获取表字段信息
        ColumnsQueryDTO columnsQueryDTO = new ColumnsQueryDTO();
        columnsQueryDTO.setConnectionName(queryDTO.getConnectionName());
        for (TableBean table : tables) {
            columnsQueryDTO.setTableName(NameCovertUtils.humpToLine(table.getName()));

            List<RowBean> tableInfo = dbDao.getTableInfo(columnsQueryDTO);
            // 寻找字段的主键，并将信息存到表信息的类中
            for (RowBean rowBean : tableInfo) {
                if (rowBean.isPrimaryKey()){
                    table.setPrimaryKeyName(rowBean.getName());
                    table.setPrimaryKeyType(rowBean.getType());
                    table.setPrimaryKeyJdbcName(rowBean.getJdbcName());
                }
            }
            table.setRows(tableInfo);
        }
        return tables;
    }

    @Override
    public boolean isOwn(String type) {
        if (ObjectUtils.nullSafeEquals(type, "MySql8")) {
            return true;
        } else {
            return ObjectUtils.nullSafeEquals(type, "MySql5");
        }
    }

    private DBInfo toDBInfo(DBConnectDTO connectDTO) {
        DBInfo dbInfo = new DBInfo();
        dbInfo.setName(connectDTO.getName());
        dbInfo.setDbName(connectDTO.getDbName());
        dbInfo.setUsername(connectDTO.getUsername());
        dbInfo.setPassword(connectDTO.getPassword());
        dbInfo.setUrl(connectDTO.getUrl());
        dbInfo.setDriverClassName(connectDTO.getDriverClassName());
        dbInfo.setDbType(connectDTO.getDbType());
        dbInfo.setComment(connectDTO.getComment());
        return dbInfo;
    }
}
